package com.jye.rseven.user.serivce.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.jye.rseven.Constants.MyConstants;
import com.jye.rseven.domain.Activities;
import com.jye.rseven.domain.Follow;
import com.jye.rseven.domain.Like;
import com.jye.rseven.domain.User;
import com.jye.rseven.user.mapper.*;
import com.jye.rseven.service.ActivitiesService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ActivitiesServiceImpl  implements ActivitiesService{

    @Autowired
    ActivitiesMapper activitiesMapper;

    @Autowired
    LikeMapper likeMapper;

    @Autowired
    FollowMapper followMapper;

    @Autowired
    TaskMapper taskMapper;

    @Autowired
    UserMapper userMapper;



    @Override
    public List<Activities> findAllActivities(int skip, int limit) {
        List<Activities> list = activitiesMapper.findAllActivities(skip,limit);
        return list;
    }

    @Override
    public Activities findActivitiesByIdAndUId(int id, int uId) {
        Activities activities = activitiesMapper.findActivitiesById(id);

        //是否参赛
        int i = activitiesMapper.isInActivities(id,uId);
        if(i !=0 ){
            //已经参加
            activities.setInActivities(true);
        }else{
            //未参加
            activities.setInActivities(false);
        }


        //是否点赞

        //常量类别
        int lType = MyConstants.likeType.activitiesType;
        //点赞数
        Like like = new Like();
        like.setType(lType);
        like.setUId(uId);
        like.setPId(id);
        like.setStatus(MyConstants.isLikeOrFollow.yes);//为点赞了
        int count = likeMapper.findLikeCountByLike(like);
        activities.setLike(count);
        int likeExist = likeMapper.isLike(like);
        if (likeExist == 0) {
            //未点赞
            activities.setIslike(false);
        } else {
            //点赞了
            activities.setIslike(true);
        }

        //是否关注
        int fType = MyConstants.followType.activitiesType;
        Follow follow = new Follow();
        follow.setType(fType);
        follow.setPId(id);
        follow.setUId(uId);
        follow.setStatus(MyConstants.isLikeOrFollow.yes);
        int followExist = followMapper.isFollow(follow);
        if (followExist == 0) {
            activities.setIsfollow(false);
        } else {
            activities.setIsfollow(true);
        }

        //用户
        activities.setUser(userMapper.findUserById(activities.getUId()));

        return activities;
    }

    @Override
    public List<Activities> findMyActivities(int uId, int skip, int limit) {
        //查到aIds
        List<Integer> aIds = activitiesMapper.findAIdsByUId(uId,skip,limit);

        //活动集合
        List<Activities> activitiesList = new ArrayList<>();
        Activities activities;
        for (Integer aId : aIds) {
            activities = activitiesMapper.findActivitiesById(aId);
            //加入集合
            activitiesList.add(activities);
        }
        return activitiesList;
    }

    @Override
    public List<Activities> findActivitiesByTitle(String title, int skip, int limit) {
        List<Activities> activitiesList = activitiesMapper.findActivitiesByTitle(title,skip,limit);
        return activitiesList;
    }

    @Override
    public List<Activities> findMyActivitesByUIdAndType(int uId,String type, int skip, int limit) {
        //查到aIds
        List<Integer> aIds = activitiesMapper.findAIdsByUId(uId,skip,limit);

        //活动集合
        List<Activities> activitiesList = new ArrayList<>();
        Activities activities;
        for (Integer aId : aIds) {
            activities = activitiesMapper.findActivitiesById(aId);
            if(activities.getType().equals(type)){
                //加入集合
                activitiesList.add(activities);
            }
        }
        return activitiesList;
    }

    @Override
    public String addActivities(Activities activities) {
        try {
            int existTitle = activitiesMapper.isExistTitle(activities.getTitle());
            if(existTitle!=0){
                return "标题名已存在!请重新输入标题!";
            }else{
                //不存在
                activities.setCreateTime(new Date());
                activities.setStatus(MyConstants.activitiesStatus.normal);
                activitiesMapper.addActivities(activities);
                return "创建成功!";
            }
        }catch (Exception e){
            return "创建失败!";
        }

    }

    @Override
    public String closeActivitiesById(int id) {
        try {
            String status = MyConstants.activitiesStatus.unavailable;
            activitiesMapper.updateStatus(id,status);
            return "修改成功!";
        }catch (Exception e){
            return "修改失败!";
        }
    }

    @Override
    public String updateActivities(Activities activities) {
        try {
            int existTitle = activitiesMapper.isExistTitle(activities.getTitle());
            if(existTitle!=0){
                return "标题名已存在!请重新输入标题!";
            }else {
                activitiesMapper.updateActivities(activities);
                return "修改成功!";
            }
        }catch (Exception e){
            return "修改失败!";
        }
    }

    @Override
    public String deleteActivitiesById(int id) {
        try {
            activitiesMapper.deleteActivitiesById(id);
            return "删除活动成功!";
        }catch (Exception e){
            return "删除活动失败!";
        }
    }

    @Override
    public List<Activities> findActivitesByType(String type, int skip, int limit) {
        List<Activities> activitiesList = activitiesMapper.findActivitesByType(type,skip,limit);
        return activitiesList;
    }

    @Override
    public Activities findActivitiesById(Integer id) {
        return activitiesMapper.findActivitiesById(id);
    }

    @Override
    public List<Activities> findAcitivitiesByTitle(String title) {
        return activitiesMapper.findAcitivitiesByTitle(title);
    }
}
